<template>
  <div class="Login_container">
    <div class="Login_box">
        <!-- 头像 -->
        <div class="avatar_box">
            <img src="@/assets/timo.jpg" alt="">
        </div>
        <!-- 登录表单 -->
        <el-form ref="LoginFormRef" :model="LoginForm" :rules="LoginRules" label-width="0" class="login_form">
            <!-- 用户名 -->
           <el-form-item prop="username">
               <el-input v-model="LoginForm.username" prefix-icon="el-icon-search"></el-input>
           </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
               <el-input v-model="LoginForm.password" type="password" prefix-icon="el-icon-search"></el-input>
           </el-form-item>
            <!-- 按钮 -->
           <el-form-item class="login_button">
             <el-button type="primary" round @click="login">登录</el-button>
             <el-button type="success" round @click="reset">重置</el-button>
           </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单绑定对象
      LoginForm: {
        username: 'admin',
        password: '123456'
      },
      //   表单规则
      LoginRules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.LoginFormRef.resetFields()
    },
    login () {
      this.$refs.LoginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.LoginForm)
        if (res.meta.status !== 200) return this.$Message.error('失误了')
        this.$Message.success('成功登录')
        console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.Login_container {
    background-color: #2b4b6b;
    height: 100%;
}
.Login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 30%;
    left: 50%;
    transform: translate(-50%);

    .avatar_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        padding: 10px;
        border-radius: 50%;
        box-shadow: 0 0 10px 0;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}
.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
}
.login_button {
    display: flex;
    justify-content: center;
}
</style>
